package com.wing.bill.controller.admin;

import com.wing.bill.dao.CabinetFormDao;
import com.wing.bill.model.response.MaterialStatisticsListVO;
import com.wing.bill.model.response.MaterialStatisticsVO;
import com.wing.common.utils.JsonResult;
import com.wing.product.service.ProductDetailService;
import com.wing.web.db.config.util.DynamicDataSource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Validated
@Api(value = "Admin-物料统计报表(按人)", tags = "#/bill/materialStatisticsReport")
@RestController("materialStatisticsReportController")
@RequestMapping("bill/admin/materialStatisticsReport")
@RequiredArgsConstructor
public class MaterialStatisticsReportController {

    private final ProductDetailService productDetailService;
    private final CabinetFormDao cabinetFormDao;

    @ApiOperation(value = "物料统计列表(按人)最小单位量", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "endTime", value = "结束时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public JsonResult<MaterialStatisticsListVO> list(String startTime, String endTime){
        Map<String, Object> params = new HashMap<>();
        setParams(params, startTime, endTime, null);
        return JsonResult.success(productDetailService.materialStatisticsReport(params));
    }

    @ApiOperation(value = "物料领新统计(按人)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "endTime", value = "结束时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "type", value = "统计类型", defaultValue = "", example = "", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/materialGetNewStatistics")
    public JsonResult<MaterialStatisticsVO> materialGetNewStatistics(String startTime, String endTime, Integer type){
        Map<String, Object> params = new HashMap<>();
        setParams(params, startTime, endTime, type);
        return JsonResult.success(productDetailService.materialGetNewStatistics(params));
    }

    @ApiOperation(value = "物料未归还统计(按人)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "endTime", value = "结束时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "type", value = "统计类型", defaultValue = "", example = "", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/materialNoReturnStatistics")
    public JsonResult<MaterialStatisticsVO> materialNoReturnStatistics(String startTime, String endTime, Integer type){
        Map<String, Object> params = new HashMap<>();
        setParams(params, startTime, endTime, type);
        return JsonResult.success(productDetailService.materialNoReturnStatistics(params));
    }

    @ApiOperation(value = "物料暂存统计(按人)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "endTime", value = "结束时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "type", value = "统计类型", defaultValue = "", example = "", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/materialTemporaryStorageStatistics")
    public JsonResult<MaterialStatisticsVO> materialTemporaryStorageStatistics(String startTime, String endTime, Integer type){
        Map<String, Object> params = new HashMap<>();
        setParams(params, startTime, endTime, type);
        return JsonResult.success(productDetailService.materialTemporaryStorageStatistics(params));
    }

    @ApiOperation(value = "物料报修统计(按人)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "endTime", value = "结束时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "type", value = "统计类型", defaultValue = "", example = "", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/materialRepairStatistics")
    public JsonResult<MaterialStatisticsVO> materialRepairStatistics(String startTime, String endTime, Integer type){
        Map<String, Object> params = new HashMap<>();
        setParams(params, startTime, endTime, type);
        return JsonResult.success(productDetailService.materialRepairStatistics(params));
    }

    @ApiOperation(value = "物料报废统计(按人)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "endTime", value = "结束时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "type", value = "统计类型", defaultValue = "", example = "", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/materialScrapStatistics")
    public JsonResult<MaterialStatisticsVO> materialScrapStatistics(String startTime, String endTime, Integer type){
        Map<String, Object> params = new HashMap<>();
        setParams(params, startTime, endTime, type);
        return JsonResult.success(productDetailService.materialScrapStatistics(params));
    }

    @ApiOperation(value = "物料报失统计(按人)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "endTime", value = "结束时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "type", value = "统计类型", defaultValue = "", example = "", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/materialLossStatistics")
    public JsonResult<MaterialStatisticsVO> materialLossStatistics(String startTime, String endTime, Integer type){
        Map<String, Object> params = new HashMap<>();
        setParams(params, startTime, endTime, type);
        return JsonResult.success(productDetailService.materialLossStatistics(params));
    }

    @ApiOperation(value = "物料异常统计(按人)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "endTime", value = "结束时间", defaultValue = "", example = "", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "type", value = "统计类型", defaultValue = "", example = "", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/materialAbnormalStatistics")
    public JsonResult<MaterialStatisticsVO> materialAbnormalStatistics(String startTime, String endTime, Integer type){
        Map<String, Object> params = new HashMap<>();
        setParams(params, startTime, endTime, type);
        return JsonResult.success(productDetailService.materialAbnormalStatistics(params));
    }

    public void setParams(Map<String, Object> params, String startTime, String endTime, Integer type){
        if(StringUtils.isNotBlank(startTime)){
            params.put("startTime", startTime);
        }
        if(StringUtils.isNotBlank(endTime)){
            params.put("endTime", endTime);
        }
        if(null != type){
            params.put("type", type);
        }
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if(StringUtils.isNotBlank(DynamicDataSource.name.get())){
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        params.put("databaseUser", databaseUser);
    }

}
